package com.lanou3g.ptp.achievement.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.lanou3g.ptp.achievement.utils.XWPFHelper;
import com.lanou3g.ptp.achievement.utils.XWPFHelperTable;
import com.lanou3g.ptp.entity.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;


public class ExportWord {

    private String newLine = System.getProperty("line.separator");

    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;

    public ExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }


    /**
     * 创建好文档的基本 标题，表格  段落等部分
     */
    //
    public XWPFDocument createXWPFDocument(Map<String, Object> result) throws FileNotFoundException {

        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc, result);
        createTableParagraph(doc, result);
        return doc;
    }

    /**
     * 创建表格的标题样式
     */
    public void createTitleParagraph(XWPFDocument document, Map<String, Object> result) throws FileNotFoundException {
        XWPFParagraph titleParagraph = document.createParagraph();//新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中

        XWPFRun titleFun0 = titleParagraph.createRun();
        titleFun0.addBreak();
        titleFun0.addBreak();
        titleFun0.addBreak();
        String imgFile = "classpath:logo.png";
        File file = ResourceUtils.getFile("classpath:logo.png");

        FileInputStream is = new FileInputStream(file);
        try {
            titleFun0.addPicture(is,
                    XWPFDocument.PICTURE_TYPE_JPEG,
                    imgFile,
                    Units.toEMU((int) Math.round(6.56 / 0.035277777)),//图片的宽
                    Units.toEMU((int) Math.round(1.62 / 0.035277777)));//图片的高
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        XWPFRun titleFun = titleParagraph.createRun();
        titleFun.addBreak();
        titleFun.setText("本 科 生 实 习 报 告");
        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(36);    //字体大小
        titleFun.setFontFamily("方正小标宋简体");//设置字体
        titleFun.addBreak();
        titleFun.addBreak();
        XWPFRun titleFun1 = titleParagraph.createRun();
        titleFun1.setText("课程名称：");
        titleFun1.setBold(true);
        titleFun1.setColor("000000");//设置颜色
        titleFun1.setFontSize(22);    //字体大小
        titleFun1.setFontFamily("仿宋_GB2312");//设置字体

        XWPFRun titleFun2 = titleParagraph.createRun();
        String str = "    ";
        titleFun2.setText(str + result.get("taskName") + str);
        titleFun2.setUnderline(UnderlinePatterns.SINGLE);
        titleFun2.setFontFamily("Times New Roman");
        titleFun2.setFontSize(22);

//        XWPFRun titleFunl2 = titleParagraph.createRun();
//        titleFunl2.setText((String) result.get("taskName"));
//        titleFunl2.setUnderline(UnderlinePatterns.SINGLE);
//        titleFunl2.setFontFamily("Times New Roman");
//        titleFunl2.setFontSize(22);

        XWPFRun titleFun3 = titleParagraph.createRun();
        titleFun3.addBreak();
        titleFun3.addBreak();
        titleFun3.addBreak();
        titleFun3.addBreak();
        titleFun3.setText("学    院：");
        titleFun3.setColor("000000");//设置颜色
        titleFun3.setFontSize(18);    //字体大小
        titleFun3.setFontFamily("仿宋_GB2312");//设置字体


        XWPFRun titleFun4 = titleParagraph.createRun();
//        titleFun4.setText(str);
//        String strr2 = (String) result.get("class");
        String strr7 = (String) result.get("schoolName");
        titleFun4.setText(str + strr7 + str);

        titleFun4.setUnderline(UnderlinePatterns.SINGLE);
        titleFun4.setFontFamily("仿宋_GB2312");
        titleFun4.setFontSize(18);

        XWPFRun titleFun5 = titleParagraph.createRun();
        titleFun5.addBreak();
        titleFun5.setText("专业班级：");
        titleFun5.setColor("000000");//设置颜色
        titleFun5.setFontSize(18);    //字体大小
        titleFun5.setFontFamily("仿宋_GB2312");//设置字体
        XWPFRun titleFun6 = titleParagraph.createRun();
        String strr2 = (String) result.get("class");
        String strr3 = (String) result.get("major");
        titleFun6.setText(str + strr3 + strr2 + str);
        titleFun6.setUnderline(UnderlinePatterns.SINGLE);
        titleFun6.setFontFamily("仿宋_GB2312");
        titleFun6.setFontSize(18);

        XWPFRun titleFun7 = titleParagraph.createRun();
        titleFun7.addBreak();
        titleFun7.setText("学生姓名：");
        titleFun7.setColor("000000");//设置颜色
        titleFun7.setFontSize(18);    //字体大小
        titleFun7.setFontFamily("仿宋_GB2312");//设置字体
        XWPFRun titleFun8 = titleParagraph.createRun();
        String strr4 = (String) result.get("studentName");
        titleFun8.setText(str + strr4 + str);
        titleFun8.setUnderline(UnderlinePatterns.SINGLE);
        titleFun8.setFontFamily("仿宋_GB2312");
        titleFun8.setFontSize(18);

        XWPFRun titleFun9 = titleParagraph.createRun();
        titleFun9.addBreak();
        titleFun9.setText("学    号：");
        titleFun9.setColor("000000");//设置颜色
        titleFun9.setFontSize(18);    //字体大小
        titleFun9.setFontFamily("仿宋_GB2312");//设置字体
        XWPFRun titleFun10 = titleParagraph.createRun();
//        titleFun10.setText(str);
        String strr6 = (String) result.get("studentNo");
        titleFun10.setText(str + strr6 + str);
        titleFun10.setUnderline(UnderlinePatterns.SINGLE);
        titleFun10.setFontFamily("仿宋_GB2312");
        titleFun10.setFontSize(18);

        XWPFRun titleFun11 = titleParagraph.createRun();
        titleFun11.addBreak();
        titleFun11.setText("实习时间：");
        titleFun11.setColor("000000");//设置颜色
        titleFun11.setFontSize(18);    //字体大小
        titleFun11.setFontFamily("仿宋_GB2312");//设置字体
        XWPFRun titleFun12 = titleParagraph.createRun();
        //titleFun12.setText(str);
        Date strr8 = (Date) result.get("date");
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String format = format1.format(strr8);
        titleFun12.setText(str + format + str);
        titleFun12.setUnderline(UnderlinePatterns.SINGLE);
        titleFun12.setFontFamily("仿宋_GB2312");
        titleFun12.setFontSize(18);

        XWPFRun titleFun13 = titleParagraph.createRun();
        titleFun13.addBreak();
        titleFun13.setText("指导教师：");
        titleFun13.setColor("000000");//设置颜色
        titleFun13.setFontSize(18);    //字体大小
        titleFun13.setFontFamily("仿宋_GB2312");//设置字体
        XWPFRun titleFun14 = titleParagraph.createRun();
        titleFun14.setText(str);
        String strr5 = (String) result.get("teacherName");
        titleFun14.setText(str + strr5 + str);
        titleFun14.setUnderline(UnderlinePatterns.SINGLE);
        titleFun14.setFontFamily("仿宋_GB2312");
        titleFun14.setFontSize(18);

        XWPFRun titleFun15 = titleParagraph.createRun();
        titleFun15.addBreak();
        titleFun15.addBreak();
        titleFun15.addBreak();
        titleFun15.addBreak();
        titleFun15.setText("大连海洋大学教务处");
        titleFun15.setBold(true);//设置字体是否加粗
        titleFun15.setColor("000000");//设置颜色
        titleFun15.setFontSize(16);    //字体大小
        titleFun15.setFontFamily("仿宋_GB2312");//设置字体
        titleFun.addBreak();    //换行
    }


    /**
     * @Description: 设置列宽和垂直对齐方式
     */
    public void setCellWidthAndVAlign(XWPFTableCell cell, String width, STVerticalJc.Enum typeEnum, STJc.Enum vAlign) {
        CTTc cttc = cell.getCTTc();

        CTTcPr cellPr = cell.getCTTc().addNewTcPr();
        cellPr.addNewVAlign().setVal(typeEnum);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(vAlign);
        CTTblWidth tblWidth = cellPr.isSetTcW() ? cellPr.getTcW() : cellPr.addNewTcW();
        if (!StringUtils.isEmpty(width)) {
            tblWidth.setW(new BigInteger(width));
            tblWidth.setType(STTblWidth.DXA);
//            tblWidth.setW(new BigInteger(String.valueOf(1526)));
        }
    }


    /**
     * 设置表格
     */
    public void createTableParagraph(XWPFDocument document, Map<String, Object> result) {

        //第一个表格
        XWPFTable infoTable = document.createTable(4, 2);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "9072", STJc.CENTER);
        //表格总宽度
        infoTable.setWidth((int) Math.round(16.5 / 0.001763158));

        infoTable.setCellMargins(0, 108, 0, 108);

        List<XWPFTableRow> rows = infoTable.getRows();
        //第一行高度
        rows.get(0).setHeight((int) Math.round(0.97 / 0.001763158));
        //第二行高度
        rows.get(1).setHeight((int) Math.round(2.11 / 0.001763158));
        //第三行高度
        rows.get(2).setHeight((int) Math.round(3.36 / 0.001763158));
        //第四行高度
        rows.get(3).setHeight((int) Math.round(17.96 / 0.001763158));

        List<XWPFTableCell> cells = rows.get(0).getTableCells();
        //第一列第一个表格宽度
        cells.get(0).getCTTc().addNewTcPr();
        setCellWidthAndVAlign(cells.get(0), "1526", STVerticalJc.CENTER, STJc.CENTER);

        XWPFParagraph paragraph1 = rows.get(0).getCell(0).getParagraphArray(0);
        paragraph1.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r1 = paragraph1.createRun();
        r1.setText("实习类型");
        r1.setBold(true);
        r1.setFontSize(14);
        r1.setFontFamily("宋体");

        XWPFParagraph paragraph2 = rows.get(0).getCell(1).getParagraphArray(0);
        XWPFRun r2 = paragraph2.createRun();
        r2.setText("课程实习□   认识实习□    生产实习□   毕业实习□");
        r2.setFontSize(14);
        r2.setFontFamily("宋体");
//        XWPFRun teachlink2 = paragraph2.createRun();
//        Integer teachlink3 = (Integer) result.get("type");
//        teachlink2.setText(String.valueOf(teachlink3));


        XWPFParagraph paragraph3 = rows.get(1).getCell(0).getParagraphArray(0);
        paragraph3.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r3 = paragraph3.createRun();
        r3.setText("实习地点:");
        r3.setBold(true);
        r3.setFontSize(14);
        r3.setFontFamily("宋体");

        XWPFParagraph paragrapho = rows.get(1).getCell(1).getParagraphArray(0);
        XWPFRun base1 = paragrapho.createRun();
        Base base = (Base) result.get("base");
        base1.setText(base.getAddress());
        base1.setFontSize(14);
        base1.setFontFamily("宋体");


        XWPFParagraph paragraph4 = rows.get(2).getCell(0).getParagraphArray(0);
        // paragraph4.setAlignment(ParagraphAlignment.CENTER);居中
        // paragraph4.setAlignment(ParagraphAlignment.LEFT);段落左对齐
        // paragraph4.setAlignment(ParagraphAlignment.BOTH);两端对齐
        XWPFRun r4 = paragraph4.createRun();
        r4.setText("目的与要求: ");
        r4.setBold(true);
        r4.setFontSize(14);
        r4.setFontFamily("宋体");
        XWPFRun teachlink1 = paragraph4.createRun();
        String teachlink = (String) result.get("target");
        teachlink1.setText(teachlink);
        teachlink1.setFontSize(14);
        teachlink1.setFontFamily("宋体");

        XWPFParagraph paragraph5 = rows.get(3).getCell(0).getParagraphArray(0);
        XWPFRun r5 = paragraph5.createRun();
        r5.setText("实习内容：");
        r5.setBold(true);
        r5.setFontSize(14);
        r5.setFontFamily("宋体");
        XWPFRun r6 = paragraph5.createRun();
        r6.setText("（包括各实习项目名称、方法手段、材料设备、过程内容、结果等）");
        r6.setFontSize(14);
        r6.setFontFamily("宋体");
        XWPFRun tasks = paragraph5.createRun();
        tasks.setText((String) result.get("taskName"));
        tasks.setFontSize(14);
        tasks.setFontFamily("宋体");

        //第二列第一个表格宽度
        //cells.get(1).getCTTc().addNewTcPr();
        //setCellWidthAndVAlign(cells.get(1), "1526", STVerticalJc.CENTER, STJc.CENTER);

        List<XWPFTableCell> cells1 = rows.get(1).getTableCells();
        //第一列第二个表格宽度
        cells.get(0).getCTTc().addNewTcPr();
        setCellWidthAndVAlign(cells1.get(0), "1526", STVerticalJc.CENTER, STJc.CENTER);


        //第二列第二个表格宽度
        //cells.get(1).getCTTc().addNewTcPr();
        //setCellWidthAndVAlign(cells1.get(1), "7833", STVerticalJc.CENTER, STJc.CENTER);

        //合并单元格
        xwpfHelperTable.mergeCellsHorizontal(infoTable, 2, 0, 1);
        xwpfHelperTable.mergeCellsHorizontal(infoTable, 3, 0, 1);


        //第二个表格
        XWPFTable infoTable1 = document.createTable(1, 1);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable1, "9072", STJc.CENTER);
        //表格总宽度
        infoTable1.setWidth((int) Math.round(16.5 / 0.001763158));
        List<XWPFTableRow> row = infoTable1.getRows();
        //表格总高度
        row.get(0).setHeight((int) Math.round(23.73 / 0.001763158));
        infoTable1.setCellMargins(0, 108, 0, 108);

        XWPFParagraph paragraph6 = row.get(0).getCell(0).getParagraphArray(0);
        XWPFRun r7 = paragraph6.createRun();
        r7.setText("实习总结：");
        r7.setBold(true);
        r7.setFontSize(14);
        r7.setFontFamily("宋体");
        XWPFRun r8 = paragraph6.createRun();
        r8.setText("（重点总结在专业学习方面的收获）");
        r8.setFontSize(14);
        r8.setFontFamily("宋体");
        r8.addBreak();//换行
        XWPFRun rsummary = paragraph6.createRun();
        Summary summary = (Summary) result.get("summary");
        rsummary.setText(summary.getContent());
        rsummary.setFontSize(14);
        rsummary.setFontFamily("宋体");
        //rsummary.setTextPosition();//行间距
        //rsummary.setIndentationFirstLine(567);//567=1厘米


        //第三个表格
        XWPFTable infoTable2 = document.createTable(3, 2);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable2, "9072", STJc.CENTER);
        infoTable2.setCellMargins(0, 108, 0, 108);
        //表格总宽度
        infoTable2.setWidth((int) Math.round(16.5 / 0.001763158));
        List<XWPFTableRow> rows1 = infoTable2.getRows();
        //第一行高度
        rows1.get(0).setHeight((int) Math.round(10.98 / 0.001763158));
        //第二行高度
        rows1.get(1).setHeight((int) Math.round(8.75 / 0.001763158));
        //第三行高度
        rows1.get(2).setHeight((int) Math.round(3.45 / 0.001763158));

        List<XWPFTableCell> cells2 = rows1.get(0).getTableCells();
        //第一列第一个表格宽度
        cells2.get(0).getCTTc().addNewTcPr();
        setCellWidthAndVAlign(cells2.get(0), "760", STVerticalJc.CENTER, STJc.CENTER);
        //第二列第一个表格宽度
//        cells2.get(1).getCTTc().addNewTcPr();
//        setCellWidthAndVAlign(cells2.get(1), "8598", STVerticalJc.CENTER, STJc.CENTER);

        List<XWPFTableCell> cells3 = rows1.get(1).getTableCells();
        //第一列第二个表格宽度
        cells3.get(0).getCTTc().addNewTcPr();
        setCellWidthAndVAlign(cells3.get(0), "760", STVerticalJc.CENTER, STJc.CENTER);
//        //第二列第二个表格宽度
//        cells3.get(0).getCTTc().addNewTcPr();
//        setCellWidthAndVAlign(cells3.get(1), "8598", STVerticalJc.CENTER, STJc.CENTER);

        List<XWPFTableCell> cells4 = rows1.get(2).getTableCells();
        //第一列第三个表格宽度
        cells4.get(0).getCTTc().addNewTcPr();
        setCellWidthAndVAlign(cells4.get(0), "760", STVerticalJc.CENTER, STJc.CENTER);
        //第二列第三个表格宽度
//        cells4.get(1).getCTTc().addNewTcPr();
//        setCellWidthAndVAlign(cells4.get(1), "8598", STVerticalJc.CENTER, STJc.CENTER);


        XWPFParagraph paragraph = rows1.get(0).getCell(0).getParagraphArray(0);
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r0 = paragraph.createRun();
        r0.setText("指导教师意见");
        r0.setBold(true);
        r0.setFontSize(14);
        r0.setFontFamily("仿宋_GB2312");

        XWPFParagraph paragraph7 = rows1.get(1).getCell(0).getParagraphArray(0);
        paragraph7.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r9 = paragraph7.createRun();
        r9.setText("实习单位意见");
        r9.setBold(true);
        r9.setFontSize(14);
        r9.setFontFamily("仿宋_GB2312");


//        XWPFParagraph paragraph = document.createParagraph();
        XWPFParagraph paragraph8 = rows1.get(2).getCell(0).getParagraphArray(0);
        paragraph8.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r10 = paragraph8.createRun();
        r10.setText("备注");
        r10.setBold(true);
        r10.setFontSize(14);
        r10.setFontFamily("仿宋_GB2312");

        XWPFParagraph paragraph9 = rows1.get(0).getCell(1).getParagraphArray(0);
        XWPFRun r11 = paragraph9.createRun();
        r11.setText("教师评语：" + newLine + newLine + newLine + newLine + newLine + newLine + newLine);
        r11.setFontSize(14);
        r11.setFontFamily("仿宋_GB2312");
        XWPFRun r12 = paragraph9.createRun();
        r12.setText("报告成绩：" + newLine);
        r12.setFontSize(14);
        r12.setFontFamily("仿宋_GB2312");
        XWPFRun r13 = paragraph9.createRun();
        r13.setText("                    指导教师签字：" + newLine);
        r13.setFontSize(14);
        r13.setFontFamily("仿宋_GB2312");
        XWPFRun r14 = paragraph9.createRun();
        r14.setText("                                          年   月   日");
        r14.setFontSize(14);
        r14.setFontFamily("仿宋_GB2312");


        XWPFParagraph paragraph10 = rows1.get(1).getCell(1).getParagraphArray(0);
        XWPFRun r15 = paragraph10.createRun();
        r15.setText(newLine + newLine + newLine + newLine + newLine + newLine + "           负责人签字：（单位盖章）" + newLine);
        r15.setFontSize(14);
        r15.setFontFamily("仿宋_GB2312");
        XWPFRun r16 = paragraph10.createRun();
        r16.setText("                                          年   月   日");
        r16.setFontSize(14);
        r16.setFontFamily("仿宋_GB2312");

        XWPFParagraph titleParagraph1 = document.createParagraph();
        XWPFRun run = titleParagraph1.createRun();
        run.setText("注：实习结束时，由实习学生填写本表后，交指导教师和实习单位签署意见，最后交所在单位归档保管");
        run.setFontSize(12);    //字体大小
        run.setFontFamily("仿宋_GB2312");//设置字体


        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for (int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            List<XWPFTableCell> cellList = infoTableRow.getTableCells();
            for (int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                //cellParagraph.setAlignment(ParagraphAlignment.CENTER);//全部居中
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(12);
                if (i % 2 != 0) {
                    cellParagraphRun.setBold(true);
                }
            }
        }
//        xwpfHelperTable.setTableHeight(infoTable, 560, STVerticalJc.CENTER);
    }

    /**
     * 往表格中填充数据
     */
    @SuppressWarnings("unchecked")
    public void exportCheckWord(Map<String, Object> dataList, XWPFDocument document, String savePath) throws IOException {
        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleFun = paragraph.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));
        List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
        XWPFTable table = document.getTableArray(0);
        //fillTableData(table, tableData);
        xwpfHelper.saveDocument(document, savePath);

    }

    /**
     * 往表格中填充数据
     */
    public void fillTableData(XWPFTable table, List<List<Object>> tableData) {
        List<XWPFTableRow> rowList = table.getRows();
        for (int i = 0; i < tableData.size(); i++) {
            List<Object> list = tableData.get(i);
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            for (int j = 0; j < list.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                cellParagraphRun.setText(String.valueOf(list.get(j)));
            }
        }
    }


    public static void main(String[] args) throws IOException {
        HashMap<String, Object> result = new HashMap<>();
        ExportWord ew = new ExportWord();
        XWPFDocument document = ew.createXWPFDocument(result);
        List<List<Object>> list = new ArrayList<List<Object>>();

        Map<String, Object> dataList = new HashMap<String, Object>();
        dataList.put("TITLE", "");
        dataList.put("TABLEDATA", list);
        ew.exportCheckWord(dataList, document, "D:\\word\\summary0501010.docx");
        System.out.println("文档生成成功");
    }


}
